package utils.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.ClassificationPageEntity;
import tools.ItemOnClickListener;
import tools.ItemOnClickListenerOther;
import volley.VolleySingleton;

/**
 * Created by dllo on 15/11/18.
 */
public class ClassificationPageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE00 = 0;
    private static final int TYPE01 = 1;
    private static final int TYPE02 = 2;
    private static final int TYPE03 = 3;
    private static final int TYPE04 = 4;
    private static final int TYPE05 = 5;

    private LayoutInflater inflater;
    private List<ClassificationPageEntity.ListEntity> entities;
    private ImageLoader imageLoader;
    private Context context;
    private ItemOnClickListenerOther itemOnClickListener;


    public void setClassIfListener(ItemOnClickListenerOther itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public ClassificationPageAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addList(List<ClassificationPageEntity.ListEntity> data) {
        entities = data;
        notifyDataSetChanged();
    }

    private boolean isListNull() {

        return entities != null && entities.size() > 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(parent.getContext());
        switch (viewType) {
            case TYPE00:
                return new ViewHolder00(inflater.inflate(R.layout.class_ification_itemo, parent, false));
            case TYPE01:
                return new ViewHolder01(inflater.inflate(R.layout.class_ification_itemf, parent, false));
            case TYPE02:
                return new ViewHolder02(inflater.inflate(R.layout.class_ification_itemt, parent, false));
            case TYPE03:
                return new ViewHolder03(inflater.inflate(R.layout.class_ification_itemg, null));
            case TYPE04:
                return new ViewHolder04(inflater.inflate(R.layout.class_ification_itemg, null));
            case TYPE05:
                return new ViewHolder05(inflater.inflate(R.layout.class_ification_itemw, parent, false));

        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (isListNull()) {
            ClassificationPageEntity.ListEntity entity = entities.get(position);
            if (holder instanceof ViewHolder00) {
                ViewHolder00 h = ((ViewHolder00) holder);
                h.classIfoImg.setImageUrl(entity.getCoverPath(), imageLoader);
                h.itemPosition = position;
            }
            if (holder instanceof ViewHolder01) {
                ViewHolder01 h1 = (ViewHolder01) holder;
                h1.classIfoImg1.setImageUrl(entities.get(1).getCoverPath(), imageLoader);
                h1.classIfoImg2.setImageUrl(entities.get(2).getCoverPath(), imageLoader);
                h1.classIfoImg3.setImageUrl(entities.get(3).getCoverPath(), imageLoader);
                h1.classIfoImg4.setImageUrl(entities.get(4).getCoverPath(), imageLoader);
                h1.Itemposition = position;
            }
            if (holder instanceof ViewHolder02) {

            }
            if (holder instanceof ViewHolder03) {
                ((ViewHolder03) holder).gonell.setVisibility(View.GONE);
            }
            if (holder instanceof ViewHolder04) {

            }
            if (holder instanceof ViewHolder05) {
                ViewHolder05 vh = (ViewHolder05) holder;
                vh.classItemTv.setText(entity.getTitle());
                vh.classItemImg.setImageUrl(entity.getCoverPath(), imageLoader);
                vh.itemPosition = position;
            }

        }
    }

    @Override
    public int getItemCount() {
        return isListNull() ? entities.size() : 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return TYPE00;
        if (position == 1)
            return TYPE01;
        if (position == 2)
            return TYPE02;
        if (position == 3)
            return TYPE03;
        if (position == 4)
            return TYPE04;

        return TYPE05;

    }

    public class ViewHolder00 extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView classIfoImg;
        private int itemPosition;

        public ViewHolder00(View itemView) {
            super(itemView);
            classIfoImg = (NetworkImageView) itemView.findViewById(R.id.classIfoImg);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (entities != null) {
                ClassificationPageEntity.ListEntity listEntity = entities.get(itemPosition);
                if (itemOnClickListener != null) {
                    itemOnClickListener.itemListener(listEntity, 0);
                }
            }
        }
    }

    public class ViewHolder05 extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView classItemImg;
        private TextView classItemTv;
        private int itemPosition;

        public ViewHolder05(View itemView) {
            super(itemView);
            classItemImg = (NetworkImageView) itemView.findViewById(R.id.classItemImg);
            classItemTv = (TextView) itemView.findViewById(R.id.classItemTv);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (entities != null) {
                ClassificationPageEntity.ListEntity listEntity = entities.get(itemPosition);
                if (itemOnClickListener != null) {
                    itemOnClickListener.itemListener(listEntity, 0);
                }
            }
        }
    }

    public class ViewHolder01 extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView classIfoImg1, classIfoImg2, classIfoImg3, classIfoImg4;
        private int Itemposition;

        public ViewHolder01(View itemView) {
            super(itemView);
            classIfoImg1 = (NetworkImageView) itemView.findViewById(R.id.classIfoImg1);
            classIfoImg2 = (NetworkImageView) itemView.findViewById(R.id.classIfoImg2);
            classIfoImg3 = (NetworkImageView) itemView.findViewById(R.id.classIfoImg3);
            classIfoImg4 = (NetworkImageView) itemView.findViewById(R.id.classIfoImg4);

            classIfoImg1.setOnClickListener(this);
            classIfoImg2.setOnClickListener(this);
            classIfoImg3.setOnClickListener(this);
            classIfoImg4.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {

            if (itemOnClickListener != null) {
                switch (v.getId()) {
                    case R.id.classIfoImg1:
                        itemOnClickListener.itemListener(entities.get(1), 1);
                        break;
                    case R.id.classIfoImg2:
                        itemOnClickListener.itemListener(entities.get(2), 2);
                        break;
                    case R.id.classIfoImg3:
                        itemOnClickListener.itemListener(entities.get(3), 3);
                        break;
                    case R.id.classIfoImg4:
                        itemOnClickListener.itemListener(entities.get(4), 4);
                        break;
                }
            }
        }
    }

    public class ViewHolder02 extends RecyclerView.ViewHolder {

        public ViewHolder02(View itemView) {
            super(itemView);

        }
    }

    public class ViewHolder03 extends RecyclerView.ViewHolder {
        private LinearLayout gonell;

        public ViewHolder03(View itemView) {
            super(itemView);
            gonell = (LinearLayout) itemView.findViewById(R.id.gonell);
        }
    }

    public class ViewHolder04 extends RecyclerView.ViewHolder {

        public ViewHolder04(View itemView) {
            super(itemView);
        }
    }

}
